package com.nf.health.app.utils;

import android.text.TextUtils;
import android.util.Log;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.exceptions.HyphenateException;
import com.nf.health.app.Constants;
import com.nf.health.app.db.DbManager;
import com.nf.health.app.models.DoctorVo;

import java.util.List;

/**
 * Created by hepann on 2016/8/25.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 环信工具类
 * @email he_pann@163.com
 */
public class EmUtil {
    private static final String TAG = "EmUtil";

    public static void loginEM(String currentUsername) {
//        String currentUser = EMClient.getInstance().getCurrentUser();
//        String localUser = PreferenceHelper.getString(PreferenceHelper.USERID,"");
//        if (!localUser.equals(currentUser)){
//            return;
//        }
        EMClient.getInstance().login(currentUsername, Constants.EM_PWD, new EMCallBack() {

            @Override
            public void onSuccess() {
                LogUtil.i(TAG, "login: onSuccess");
                //从服务器上获取,保存在本地
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
                            // ** 第一次登录或者之前logout后再登录，加载所有本地群和回话
                            // ** manually load all local groups and
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                // ** 第一次登录或者之前logout后再登录，加载所有本地群和回话
                // ** manually load all local groups and
//				EMClient.getInstance().groupManager().loadAllGroups();
//				EMClient.getInstance().chatManager().loadAllConversations();

                String nick = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
                // 更新当前用户的nickname 此方法的作用是在ios离线推送时能够显示用户nick
//				boolean updatenick = EMClient.getInstance().updateCurrentUserNick(nick);
                //异步获取当前用户的昵称和头像(从自己服务器获取，demo使用的一个第三方服务)
//				DemoHelper.getInstance().getUserProfileManager().asyncGetCurrentUserInfo();

//                // 进入主页面
//                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                startActivity(intent);
            }

            @Override
            public void onProgress(int progress, String status) {
                Log.e(TAG, "login: onProgress");
            }

            @Override
            public void onError(final int code, final String message) {
                Log.e(TAG, "login: onError>> " + message);
            }
        });
    }

    /**
     * 把医生类保存成联系人
     *
     * @param list
     */
    public static void saveDoctorVoAsContact(List<DoctorVo> list) {
        EaseUser ease;
        for (DoctorVo user : list) {// addGroupManagerList
            ease = new EaseUser(user.getDoctorid());
            ease.setNick(TextUtils.isEmpty(user.getDoctorname()) ? user.getNick() : user.getDoctorname());
            ease.setAvatar(user.getHimg());
            DbManager.getInstance().saveContact(ease);
        }
    }
}
